# coding: utf-8
from sqlalchemy import Column, DateTime, Enum, ForeignKey, Index, String, Table, Text, text
from sqlalchemy.dialects.mysql import BIT, INTEGER
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class AuthPerm(Base):
    __tablename__ = 'auth_perm'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))

    users = relationship('AuthUser', secondary='auth_user_perm')


class AuthRole(Base):
    __tablename__ = 'auth_role'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)
    craete_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


t_auth_role_perm = Table(
    'auth_role_perm', metadata,
    Column('role_id', INTEGER(10), nullable=False),
    Column('perm_id', INTEGER(10), nullable=False)
)


class AuthUser(Base):
    __tablename__ = 'auth_user'

    id = Column(INTEGER(10), primary_key=True)
    username = Column(String(50), nullable=False)
    password = Column(String(50), nullable=False)
    is_disabled = Column(Enum('Y', 'N'), nullable=False, server_default=text("'N'"))
    created_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    updated_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
    deleted_at = Column(DateTime)


t_auth_user_role = Table(
    'auth_user_role', metadata,
    Column('user_id', INTEGER(10), nullable=False),
    Column('role_id', INTEGER(10), nullable=False)
)


class BackendLog(Base):
    __tablename__ = 'backend_log'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    username = Column(String(50), nullable=False)
    operation = Column(String(50), nullable=False)
    craeted_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class BackendProject(Base):
    __tablename__ = 'backend_project'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)
    info = Column(Text, nullable=False)


class BackendSetting(Base):
    __tablename__ = 'backend_settings'

    id = Column(INTEGER(10), primary_key=True)
    key = Column(String(50), nullable=False)
    value = Column(String(50), nullable=False)


class BackendUser(Base):
    __tablename__ = 'backend_user'

    id = Column(INTEGER(10), primary_key=True)
    username = Column(String(50), nullable=False)
    password = Column(String(50), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class BasicLocation(Base):
    __tablename__ = 'basic_location'

    id = Column(INTEGER(10), primary_key=True)


class Comment(Base):
    __tablename__ = 'comment'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    message = Column(String(300), nullable=False)
    target_type = Column(String(50), nullable=False)
    target_id = Column(INTEGER(10), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class EcCategory(Base):
    __tablename__ = 'ec_category'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)


class EcOrder(Base):
    __tablename__ = 'ec_order'

    id = Column(INTEGER(10), primary_key=True)
    price = Column(INTEGER(11), nullable=False)


class EcOrderProduct(Base):
    __tablename__ = 'ec_order_product'

    id = Column(INTEGER(10), primary_key=True)
    order_id = Column(INTEGER(10), nullable=False)
    product_id = Column(INTEGER(10), nullable=False)
    price = Column(INTEGER(10), nullable=False)


class EcProduct(Base):
    __tablename__ = 'ec_product'

    id = Column(INTEGER(10), primary_key=True)
    shop_id = Column(INTEGER(10), nullable=False)
    shop_name = Column(String(50), nullable=False)
    category_id = Column(INTEGER(10), nullable=False)
    category_name = Column(String(50), nullable=False)
    name = Column(String(50), nullable=False)
    price = Column(INTEGER(11), nullable=False, comment='价格')


class EcShop(Base):
    __tablename__ = 'ec_shop'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)


class NewsColumn(Base):
    __tablename__ = 'news_column'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)
    parent_id = Column(ForeignKey('news_column.id'), index=True)
    level = Column(BIT(1), nullable=False)

    parent = relationship('NewsColumn', remote_side=[id])


class NewsNew(Base):
    __tablename__ = 'news_news'

    id = Column(INTEGER(10), primary_key=True)
    column_id = Column(INTEGER(10), nullable=False)
    author = Column(String(50), nullable=False)
    message = Column(Text, nullable=False)
    publish_time = Column(DateTime)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class NewsTag(Base):
    __tablename__ = 'news_tag'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)


class NewsTagNew(Base):
    __tablename__ = 'news_tag_news'

    id = Column(INTEGER(10), primary_key=True)
    tag_id = Column(INTEGER(10), nullable=False)
    news_id = Column(INTEGER(10), nullable=False)


class SearchLog(Base):
    __tablename__ = 'search_log'

    id = Column(INTEGER(10), primary_key=True)
    keyword = Column(String(300), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class SearchMapping(Base):
    __tablename__ = 'search_mapping'

    id = Column(INTEGER(10), primary_key=True)
    target_type = Column(String(50), nullable=False)
    target_id = Column(INTEGER(10), nullable=False)
    word_id = Column(INTEGER(10), nullable=False)
    count = Column(INTEGER(10), nullable=False, server_default=text("'1'"))
    weight = Column(INTEGER(10), nullable=False, server_default=text("'1'"))


class SearchWord(Base):
    __tablename__ = 'search_word'

    id = Column(INTEGER(10), primary_key=True)
    word = Column(String(50))


class Session(Base):
    __tablename__ = 'session'

    id = Column(INTEGER(10), primary_key=True)
    key = Column(String(50), nullable=False)
    data = Column(Text, nullable=False)
    expired_at = Column(DateTime, nullable=False, index=True)


class Strategy(Base):
    __tablename__ = 'strategy'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)


class SystemCron(Base):
    __tablename__ = 'system_cron'

    id = Column(INTEGER(10), primary_key=True)
    time = Column(String(50), nullable=False)
    task = Column(String(50), nullable=False)
    last_exec = Column(DateTime)
    next_exec = Column(DateTime)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class SystemNotify(Base):
    __tablename__ = 'system_notify'

    id = Column(INTEGER(10), primary_key=True)
    type = Column(String(50), nullable=False, comment='通知类型')
    user_id = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
    message = Column(String(300), nullable=False)
    extras = Column(String(300), nullable=False, comment='附加信息（JSON）')
    is_readed = Column(Enum('Y', 'N'), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class SystemSetting(Base):
    __tablename__ = 'system_settings'

    id = Column(INTEGER(10), primary_key=True)
    key = Column(String(50), nullable=False, unique=True)
    value = Column(String(300))
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class UserLog(Base):
    __tablename__ = 'user_log'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    operation = Column(String(50), nullable=False, comment='登录，修改密码')
    extras = Column(String(300), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class UserProfile(Base):
    __tablename__ = 'user_profile'

    id = Column(INTEGER(10), primary_key=True)


class UserSetting(Base):
    __tablename__ = 'user_settings'

    id = Column(INTEGER(10), primary_key=True)
    key = Column(String(50), nullable=False, unique=True)
    value = Column(String(300))
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class VipLog(Base):
    __tablename__ = 'vip_log'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    type = Column(String(50), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class VipOrder(Base):
    __tablename__ = 'vip_order'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    username = Column(String(50), nullable=False)
    pay_amount = Column(INTEGER(11), nullable=False, comment='支付金额')
    pay_method = Column(String(50), nullable=False, comment='支付方式')
    pay_status = Column(INTEGER(11), nullable=False, comment='支付状态')
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class ZbAccount(Base):
    __tablename__ = 'zb_account'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    type = Column(INTEGER(10), nullable=False)
    name = Column(String(50), nullable=False)
    amount = Column(INTEGER(10), nullable=False, server_default=text("'0'"))
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class ZbBill(Base):
    __tablename__ = 'zb_bill'

    id = Column(INTEGER(10), primary_key=True)
    book_id = Column(INTEGER(10), nullable=False, comment='账本')
    category_id = Column(INTEGER(10), nullable=False, comment='账单类别')
    category_name = Column(String(50), nullable=False, comment='账单类别名称')
    account_id = Column(INTEGER(10), nullable=False, comment='交易账户')
    peer_account_id = Column(INTEGER(10), comment='对方账户（转账）')
    service_charge = Column(INTEGER(10), nullable=False, server_default=text("'0'"), comment='交易手续费')
    trade_type = Column(Enum('in', 'out', 'transfer'), nullable=False, comment='交易类别')
    amount = Column(INTEGER(10), nullable=False, comment='账单金额')
    time = Column(DateTime, nullable=False, comment='账单时间')
    remark = Column(String(300), nullable=False, comment='备注')
    creator = Column(INTEGER(10), nullable=False, comment='记账人')
    created_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    updated_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))


class ZbBillChangelog(Base):
    __tablename__ = 'zb_bill_changelog'

    id = Column(INTEGER(10), primary_key=True)
    bill_id = Column(INTEGER(10), nullable=False)
    changes = Column(String(300), nullable=False, comment='JSON')
    user_id = Column(INTEGER(10), nullable=False)
    created_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class ZbBook(Base):
    __tablename__ = 'zb_book'

    id = Column(INTEGER(10), primary_key=True)
    name = Column(String(50), nullable=False)
    creator = Column(INTEGER(10), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))

    users = relationship('AuthUser', secondary='zb_book_user')


class ZbBookCategory(Base):
    __tablename__ = 'zb_book_category'

    id = Column(INTEGER(10), primary_key=True)
    book_id = Column(INTEGER(10), nullable=False, comment='账本')
    type = Column(Enum('IN', 'OUT'), nullable=False, server_default=text("'OUT'"))
    name = Column(String(50), nullable=False)
    icon = Column(String(100))
    order = Column(INTEGER(10), nullable=False, server_default=text("'0'"))
    is_hiden = Column(Enum('Y', 'N'), nullable=False, server_default=text("'N'"))
    can_update = Column(Enum('Y', 'N'), nullable=False, server_default=text("'Y'"))
    parent_id = Column(INTEGER(10))
    level = Column(BIT(1), nullable=False)


class ZbCategory(Base):
    __tablename__ = 'zb_category'

    id = Column(INTEGER(10), primary_key=True)
    type = Column(Enum('IN', 'OUT'), nullable=False, server_default=text("'OUT'"))
    name = Column(String(50), nullable=False)
    icon = Column(String(100))
    order = Column(INTEGER(10), nullable=False, server_default=text("'0'"))
    is_hiden = Column(Enum('Y', 'N'), nullable=False, server_default=text("'N'"))
    can_update = Column(Enum('Y', 'N'), nullable=False, server_default=text("'Y'"))
    parent_id = Column(INTEGER(10))
    level = Column(BIT(1), nullable=False)


class ZbUser(Base):
    __tablename__ = 'zb_user'

    id = Column(INTEGER(10), primary_key=True)
    user_id = Column(INTEGER(10), nullable=False)
    debts = Column(INTEGER(10), nullable=False)
    net_asset = Column(INTEGER(10), nullable=False)
    total_asset = Column(INTEGER(10), nullable=False)


t_auth_user_perm = Table(
    'auth_user_perm', metadata,
    Column('user_id', ForeignKey('auth_user.id'), nullable=False, index=True),
    Column('perm_id', ForeignKey('auth_perm.id'), nullable=False, index=True)
)


class BackendNotify(Base):
    __tablename__ = 'backend_notify'

    id = Column(INTEGER(10), primary_key=True)
    type = Column(String(50), nullable=False, comment='通知类型')
    user_id = Column(ForeignKey('backend_user.id'), nullable=False, index=True)
    message = Column(String(300), nullable=False)
    extras = Column(String(300), nullable=False, comment='附加信息（JSON）')
    is_readed = Column(Enum('Y', 'N'), nullable=False)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    update_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))

    user = relationship('BackendUser')


t_zb_book_user = Table(
    'zb_book_user', metadata,
    Column('book_id', ForeignKey('zb_book.id'), nullable=False),
    Column('user_id', ForeignKey('auth_user.id'), nullable=False, index=True),
    Index('book_id_user_id', 'book_id', 'user_id', unique=True)
)
